﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Devices.DeviceEntity
{
    /// <summary>
    /// 一氧化碳检测器设备类型
    /// </summary>
    public partial class COVI : DeviceBase
    {
        decimal comax;
        decimal comin;
        decimal vimax;
        decimal vimin;

        decimal co;
        decimal vi;

        string deviceId;

        //string statusDescription;

        /// <summary>
        /// 一氧化碳检测器设备构造方法
        /// </summary>
        public COVI()
        {
            InitializeComponent();
            this.Size = new Size(32, 32);
            //this.BringToFront();
        }
        /// <summary>
        /// 设备状态描述
        /// </summary>
        public override string StatusDescription
        {
            get
            {
                return base.StatusDescription;
            }

            set
            {
                switch (value)
                {
                    case "正常":
                        {
                            this.Image = ResPic.一氧化碳检测器正常;
                        }
                        break;
                    case "故障":
                        {
                            this.Image = ResPic.一氧化碳检测器故障;
                        }
                        break;
                }
                base.StatusDescription = value;
            }
        }
        /// <summary>
        /// 一氧化碳最大值
        /// </summary>
        public decimal Comax
        {
            get
            {
                return comax;
            }

            set
            {
                comax = value;
                this.coviPanelShow.comax.Text = value.ToString();
            }
        }
        /// <summary>
        /// 一氧化碳最小值
        /// </summary>
        public decimal Comin
        {
            get
            {
                return comin;
            }

            set
            {
                comin = value;
                this.coviPanelShow.comin.Text = value.ToString();
            }
        }
        /// <summary>
        /// 能见度最大值
        /// </summary>
        public decimal Vimax
        {
            get
            {
                return vimax;
            }

            set
            {
                vimax = value;
                this.coviPanelShow.vimax.Text = value.ToString();
            }
        }
        /// <summary>
        /// 能见度最小值
        /// </summary>
        public decimal Vimin
        {
            get
            {
                return vimin;
            }

            set
            {
                vimin = value;
                this.coviPanelShow.vimin.Text = value.ToString();
            }
        }
        /// <summary>
        /// 当前一氧化碳值
        /// </summary>
        public decimal Co
        {
            get
            {
                return co;
            }

            set
            {
                co = value;
                this.coviPanelShow.covalue.Text = value.ToString();
                CoViValueChanged();
            }
        }
        /// <summary>
        /// 当前Vi值
        /// </summary>
        public decimal Vi
        {
            get
            {
                return vi;
            }

            set
            {
                vi = value;
                this.coviPanelShow.vivalue.Text = value.ToString();
                CoViValueChanged();
            }
        }
        /// <summary>
        /// 设备ID
        /// </summary>
        public override string DeviceId
        {
            get
            {
                return deviceId;
            }

            set
            {
                deviceId = value;
                ReadMaxMin();
            }
        }
        private DateTime CoAlarmTime
        {
            get; set;
        }
        private DateTime ViAlarmTime
        {
            get; set;
        }
        int coviAlarmTime = Properties.Settings.Default.AlarmTimeCOVE;
        private void Alarm(string alarmType, string alarmInfo)
        {
            if (alarmType == "co")
            {
                TimeSpan timeSpan = DateTime.Now.Subtract(CoAlarmTime);
                int time = timeSpan.Seconds;
                if (time >= coviAlarmTime)
                {
                    InsertDbAlarm(DeviceId, DateTime.Now, alarmInfo);
                }
                CoAlarmTime = DateTime.Now;
                DataDeal.ShowMessage($"设备[{ Name}] :{alarmInfo}");
            }
            if (alarmType == "vi")
            {
                TimeSpan timeSpan = DateTime.Now.Subtract(ViAlarmTime);
                int time = timeSpan.Seconds;
                if (time >= coviAlarmTime)
                {
                    InsertDbAlarm(DeviceId, DateTime.Now, alarmInfo);
                }
                ViAlarmTime = DateTime.Now;
                DataDeal.ShowMessage($"设备[{ Name}] :{alarmInfo}");
            }
        }
        private void InsertDbAlarm(string deviceId, DateTime alarmTime, string alarmInfo)
        {
            DBHelper sdh = new DBHelper();
            string updatesql = "insert into H_AlarmInfo(AlarmID,DeviceID,AlarmTime,AlarmInfo) values ('" + DBHelper.GetGuid + "','" + deviceId + "','" + alarmTime.ToString() + "','" + alarmInfo + "')";
            sdh.RunCommandAsync(updatesql);
        }

        private void CoViValueChanged()
        {
            if (Co > Comax)
            {
                if (!(coviPanelShow.covalue.ForeColor == Color.Red))
                {
                    coviPanelShow.covalue.ForeColor = Color.Red;
                    Alarm("co", "一氧化碳值超越上限:" + Co.ToString());

                }
            }
            //CO值超下界
            if (Co < Comin)
            {
                if (!(coviPanelShow.covalue.ForeColor == Color.Blue))
                {
                    coviPanelShow.covalue.ForeColor = Color.Blue;
                    Alarm("co", "一氧化碳值低于下限:" + Co.ToString());

                }
            }
            //CO值恢复
            if (Co <= Comax && Co >= Comin)
            {
                coviPanelShow.covalue.ForeColor = Color.White;
            }
            //VI值超上界
            if (Vi > Vimax)
            {
                if (!(coviPanelShow.vivalue.ForeColor == Color.Red))
                {
                    coviPanelShow.vivalue.ForeColor = Color.Red;
                    Alarm("vi", "可见度值高于上限:" + Vi.ToString());

                }
            }
            //VI值超上界
            if (Vi < Vimin)
            {
                if (!(coviPanelShow.vivalue.ForeColor == Color.Blue))
                {
                    coviPanelShow.vivalue.ForeColor = Color.Blue;
                    Alarm("vi", "可见度值低于下限:" + Vi.ToString());

                }
            }
            //VI值恢复
            if (Vi <= Vimax && Vi >= Vimin)
            {
                coviPanelShow.vivalue.ForeColor = Color.White;
            }
        }

        private void ShowPanel()
        {
            this.coviPanelShow.Parent = this.Parent;
            this.coviPanelShow.Location = new Point(this.Location.X - 84, this.Location.Y + 30);
            this.coviPanelShow.BringToFront();
            this.coviPanelShow.Show();
        }

        private async void ReadMaxMin()
        {
            DBHelper dh = new DBHelper();
            DataTable dtMaxMin = await dh.GetTableAsync("select * from M_ParamLimit where DeviceTypeID='COVI'");
            foreach (DataRow dr in dtMaxMin.Rows)
            {
                if (dr["ParamType"].ToString() == "CO")
                {
                    Comax = Convert.ToDecimal(dr["HighAlrLimit"].ToString());
                    Comin = Convert.ToDecimal(dr["LowAlrLimit"].ToString());
                }
                if (dr["ParamType"].ToString() == "VI")
                {
                    Vimax = Convert.ToDecimal(dr["HighAlrLimit"].ToString());
                    Vimin = Convert.ToDecimal(dr["LowAlrLimit"].ToString());
                }

            }
        }

        public override void SendCommand(CommandType commandType)
        {
            throw new NotImplementedException();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            ShowPanel();

        }
    }
}
